#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: xiaodun
@date: 2025/9/1820:36
"""
from selenium.webdriver.common.by import By
from dl_auto_test.pages.base_page import BasePage
from dl_auto_test.config.config import config

class LoginPage(BasePage):
    USERNAME_INPUT=(By.ID,"userno")
    PASSWORD_INPUT=(By.ID,"pwd")
    LOGIN_BUTTON=(By.ID,"dl_btn")
    ERROR_MESSAGE=(By.CLASS_NAME,"zbox-popup-text")
    SUCCESS_MESSAGE=(By.CLASS_NAME,"thickness-dialog yz_modelgdt")

    def __init__(self,driver):
        super().__init__(driver)
        self.login_url=f"{self.base_url}/Login.html?loginType=02"

    def open_login_page(self):
        self.open(self.login_url)

    def enter_username(self,username):
        self.input_text(self.USERNAME_INPUT,username)

    def enter_password(self,password):
        self.input_text(self.PASSWORD_INPUT,password)

    def click_login(self):
        self.click(self.LOGIN_BUTTON)

    def login(self,username=None,password=None):
        if username is None:
            username=config.get_username()
        if password is None:
            password=config.get_password()

        self.open_login_page()
        self.enter_username(username)
        self.enter_password(password)
        self.click_login()

    def get_error_message(self):
        if self.is_element_present(self.ERROR_MESSAGE):
            return self.get_text(self.ERROR_MESSAGE)
        return ""

    def get_success_message(self):
        if self.is_element_present(self.SUCCESS_MESSAGE):
            return self.get_text(self.SUCCESS_MESSAGE)
        return ""

    def is_login_successfull(self):
        return self.driver.current_url!=self.login_url and "gys/index" in self.driver.current_url